package com.innerchic.mapper.file;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Base64;

import com.innerchic.mapper.log.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：young
 * 日    期：2021/11/4 10:17
 * 包    名：com.innerchic.mapper.file
 * 描    述：快速的文件读取
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class FastFileReader {

    /**
     * 读文件
     * 一次性读取文件
     *
     * @param file 文件对象
     */
    public static byte[] reader(final File file) {
        InputStream in = null;
        try {
            byte[] bytes;
            in = new FileInputStream(file);
            bytes = new byte[(int) file.length()];
            int read = in.read(bytes);
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 行读取接口
     */
    public interface OnReadLine {
        void readLine(String line);
    }

    /**
     * 读取文件Base64
     *
     * @param file 文件对象
     */
    public static String readerBase64(File file) {
        return Base64.encodeToString(reader(file), Base64.DEFAULT);
    }

    /**
     * 读取一行
     *
     * @param fileInputStream 文件流
     * @param onReadLine      行读取接口
     */
    public static void readLine(InputStream fileInputStream, OnReadLine onReadLine) {
        if (onReadLine == null) {
            Logger.d("readLine()读取接口为空");
            return;
        }
        if (fileInputStream == null) {
            Logger.d("readLine()读取文件流不存在");
            return;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(
                            fileInputStream, StandardCharsets.UTF_8));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.trim().length() == 0) continue;
                onReadLine.readLine(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取assets中的资源文件
     *
     * @param context  上下文
     * @param fileName 文件名
     * @param readLine 读取接口
     */
    public static void readLine(Context context, String fileName, OnReadLine readLine) {
        if (readLine == null) return;
        AssetManager assetManager = context.getAssets();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), StandardCharsets.UTF_8));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                readLine.readLine(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取一行
     *
     * @param file       文件
     * @param onReadLine 行读取接口
     */
    public static void readLine(File file, OnReadLine onReadLine) {
        if (onReadLine == null) {
            Logger.d("readLine()读取接口为空");
            return;
        }
        if (file == null || !file.exists()) {
            Logger.d("readLine()读取文件不存在");
            return;
        }
        try {
            readLine(new FileInputStream(file), onReadLine);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取sql文件
     *
     * @param context  上下文对象
     * @param fileName 如果以@起始则默认读取assets/sql下的文件,反之则是正常路径
     *                 文件请以标准.sql结尾
     * @return String字符串
     */
    public static String readSql(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();

        //解析名字
        if (fileName.startsWith("@")) {
            fileName = "sql/" + fileName.substring(1);
            if (!fileName.endsWith(".sql")) fileName = fileName + ".sql";
        }

        //使用IO流读取json文件内容
        try {
            readLine(assetManager.open(fileName), line -> {
                if (line.trim().length() == 0) return;
                if (line.trim().startsWith("##")) return;
                if (line.trim().startsWith("--")) return;
                stringBuilder.append(line);
            });
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return stringBuilder.toString();
    }

}
